package com.zqw.service.impl;

import com.zqw.info.AddressInfo;
import com.zqw.info.GoodsInfo;
import com.zqw.info.OrderDetailsInfo;
import com.zqw.info.OrderInfo;
import com.zqw.mapper.OrderMapper;
import com.zqw.mapper.ShopCarMapper;
import com.zqw.pojo.*;
import com.zqw.service.OrderService;
import com.zqw.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ShopCarMapper shopCarMapper;

    //生成一个订单
    public String addOrder(List<GoodsInfo> shopCar, int cid,String[] goods, String addid) {
        Iterator<GoodsInfo> iterator = shopCar.iterator();
        List<OrderDetailsInfo> orderDetailsInfos = new ArrayList<>();
        String uuid = UUIDUtil.getUUID();
        OrderDetailsInfo orderDetailsInfo;
        float sum = 0;
        //循环获取总价，并将需要转成订单的保存到订单详情的list中
        while (iterator.hasNext()){
            GoodsInfo next = iterator.next();
            for (int j = 0; j < goods.length; j++) {
                if(next.getGoodsId().equals(goods[j])){
                    orderDetailsInfo = new OrderDetailsInfo();
                    orderDetailsInfo.setOid(uuid);
                    orderDetailsInfo.setGid(goods[j]);
                    orderDetailsInfo.setOdid(UUIDUtil.getUUID());
                    orderDetailsInfo.setOdprice(next.getGoodsPrice());
                    orderDetailsInfo.setOdnumber(next.getGoodsNumber());
                    orderDetailsInfos.add(orderDetailsInfo);
                    sum+=next.getGoodsNumber()*next.getGoodsPrice()*next.getGoodsDiscount();
                    break;
                }
            }
        }
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOid(uuid);
        orderInfo.setCid(cid);
        orderInfo.setOcountPrice(sum);
        orderInfo.setAddid(addid);
        boolean b = orderMapper.addOrder(orderInfo);
        Map<String, String> map;
        if(b){
            b = orderMapper.addOrderDetails(orderDetailsInfos);
            if (b){
                iterator = shopCar.iterator();
                while (iterator.hasNext()){
                    GoodsInfo next = iterator.next();
                    for (int j = 0; j < goods.length; j++) {
                        if(next.getGoodsId().equals(goods[j])){
                            map = new HashMap<>();
                            map.put("cid",String.valueOf(cid));
                            map.put("gid",goods[j]);
                            map.put("update_time",String.valueOf((new Date().getTime())/1000L));
                            shopCarMapper.delShopCar(map);
                            iterator.remove();
                            break;
                        }
                    }
                }
                return uuid;
            }
        }
        return null;
    }

    /**
     * 根据id查询订单
     * @param oid
     * @return
     */
    public OrderPojo queryOrder(String oid){
        return orderMapper.queryOrder(oid);
    }

    /**
     * 查询所有的订单
     * @param
     * @return
     */
    public List<OrderPojo> queryOrders(String cid){
        return orderMapper.queryOrders(cid);
    }

    /**
     * 修改订单
     * @param orderInfo
     * @return
     */
    public boolean updOrders(OrderInfo orderInfo){
        return orderMapper.updOrders(orderInfo);
    }

    /**
     * 查询地址
     * @param cid
     * @return
     */
    public List<AddressPojo> queryAddress(String cid){
        return orderMapper.queryAddress(cid);
    }

    /**
     * 添加一个收货地址
     * @param addressInfo
     * @return
     */
    public boolean addAddress(AddressInfo addressInfo){
        return orderMapper.addAddress(addressInfo);
    }

    /**
     * 查询所有省
     * @return
     */
    public List<ProvincePojo> queryProvince(){
        return orderMapper.queryProvince();
    }

    /**
     * 根据id查询所有的市
     * @return
     */
    public List<CityPojo> queryCity(String id){
        return orderMapper.queryCity(id);
    }

    /**
     * 根据id查询所有的区
     * @return
     */
    public List<DistrictPojo> queryDistrict(String id){
        return orderMapper.queryDistrict(id);
    }

    /**
     * 支付
     * @param payment
     * @param oid
     * @return
     */
    public boolean payment(String payment,String oid){
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOid(oid);
        orderInfo.setOstatus(1);
        boolean b = orderMapper.updOrders(orderInfo);
        return b;
    }

    /**
     * 根据id删除地址
     * @param addid
     * @return
     */
    public boolean delAddr(String addid){
        return orderMapper.delAddr(addid);
    }

}
